package com.comdo.newup_loan.module.limit;

import com.comdo.newup_loan.http.RetrofitHelper;
import com.comdo.newup_loan.model.CarBrandBean;
import com.comdo.newup_loan.model.CarModelBean;
import com.comdo.newup_loan.model.CarSeriesBean;
import com.comdo.newup_loan.model.Result;
import com.comdo.newup_loan.util.StringUtils;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import okhttp3.RequestBody;

/**
 * Created by liyuan on 2017/11/7.
 */

class CarBrandPersenter extends CarBrandContact.Presenter{
    @Override
    public void getList() {

        mView.showLoadingDialog("");
        mCompositeDisposable.add(RetrofitHelper.createMyApi().getAllBrandList()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<List<CarBrandBean>>>() {
                    @Override
                    public void accept(Result<List<CarBrandBean>> Result) throws Exception {
                        mView.dismissLoadingDialog();
                        if(Result.getCode()==1){
                            mView.getList(Result.getResult());
                        }else if(Result.getCode()==-3){
                            mView.getFail("您的登陆已经失效，请重新登陆");
                            mView.exitLogin();
                        }else{
                           mView.getFail(Result.getMessage());
                        }

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.dismissLoadingDialog();
                        mView.getFail("请求失败");
                    }
                }));
    }
    @Override
    public void getSeriesList(int brandId) {
        Gson gson = new Gson();
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("brandId", brandId);
        map.put("sign", StringUtils.SortStringAndAddKey(map));
        String strEntity = gson.toJson(map);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"), strEntity);
        mCompositeDisposable.add(RetrofitHelper.createMyApi().getAllCarSeriesFromBrand(body)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<List<CarSeriesBean>>>() {
                    @Override
                    public void accept(Result<List<CarSeriesBean>> Result) throws Exception {
                        if(Result.getCode()==1){
                            mView.getSeriesList(Result.getResult());
                        }else if(Result.getCode()==-3){
                            mView.getFail("您的登陆已经失效，请重新登陆");
                            mView.exitLogin();
                        }else{
                            mView.getFail(Result.getMessage());
                        }

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.getFail("请求失败");
                    }
                }));
    }

    @Override
    public void getModelList(int seriesId) {
        Gson gson = new Gson();
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("seriesId", seriesId);
        map.put("sign", StringUtils.SortStringAndAddKey(map));
        String strEntity = gson.toJson(map);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"), strEntity);
        mCompositeDisposable.add(RetrofitHelper.createMyApi().getAllCarModel(body)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<List<CarModelBean>>>() {
                    @Override
                    public void accept(Result<List<CarModelBean>> Result) throws Exception {
                        if(Result.getCode()==1){
                            mView.getModelList(Result.getResult());
                        }else if(Result.getCode()==-3){
                            mView.getFail("您的登陆已经失效，请重新登陆");
                            mView.exitLogin();
                        }else{
                            mView.getFail(Result.getMessage());
                        }

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.getFail("请求失败");
                    }
                }));
    }
}
